<template>
  <div class="app-container">
    <el-form ref="placeCodeForm" v-loading="formLoading" :rules="rules" :model="form" label-position="top">

      <el-row type="flex" :gutter="20">
        <el-col :span="24">
          <el-form-item label="管理办法标题" prop="title" style="width: 400px">
            <el-input v-model="form.title" placeholder="请输入管理办法标题" clearable  />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" :gutter="20">
        <el-col :span="24">
          <el-form-item label="管理办法内容" prop="Abstract">
            <div class="container">
              <Toolbar :editor="editor" :default-config="toolbarConfig" :mode="mode" />
              <Editor v-model="form.content" :default-config="editorConfig" :mode="mode" @onCreated="onCreated" />
            </div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import {editIncubatorsArticleApi, getIncubatorsArticleByFatherIdApi} from "@/api/entrepreneurship";
import {uploadApi} from "@/api/user";
export default {
  components: { Editor, Toolbar },
  name: 'PlaceCodeDetail',
  data(){
    return{
      editor: null,
      editor2: null,
      liveTime:[],
      comList:{},
      userList:{},
      publishList:{},
      toolbarConfig: {
        excludeKeys: [
          // 排除菜单组，写菜单组 key 的值即可
          'group-video' // 去掉视频
        ]
      },
      toolbarConfig2: {
        excludeKeys: [
          // 排除菜单组，写菜单组 key 的值即可
          'group-video' // 去掉视频
        ]
      },
      editorConfig: { MENU_CONF: {
          // 配置上传图片
          uploadImage: {
            customUpload: this.uploadImageMe // this.uploadImageMe 是 methods 中的一个普通方法
          },
        },
        placeholder: ''
      },
      editorConfig2: { MENU_CONF: {}, placeholder: '' },
      mode: 'default',
      mode2: 'default',
      uploadUrl: process.env.VUE_APP_UPLOAD_API,
      dialogImageUrl: '',
      dialogVisible: false,
      form: {
        id: null,
        title: '',
        content: '',
        fatherId: 0,
        type: 1
      },

      rules: {
        title:[{ required: true, trigger: 'blur', message: '请填写管理办法标题' }],
        content:[{ required: true, trigger: 'blur', message: '请填写管理办法内容' }],

      },
      formLoading: false,
    }
  },
  props:['id','closeDialog'],
  methods:{
    //上传文件配置
    uploadImageMe(file, insertFn){
      // file 即选中的文件
      // 自己实现上传，并得到图片 url alt href
      let name = file.name;
      let suffix = file.type.split("/")[1];
      name = name + '.' + suffix;
      let formdata = new FormData()
      formdata.append("file", file)
      //上传方法
      uploadApi(formdata).then(res => {
        console.log(res)
        // 最后插入图片
        insertFn(res , res, res )
      })

    },
    handleAvatarSuccess(res, file) {
      this.form.logo = res.data;
    },
    onCreated(editor) {
      this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
    },
    onCreated2(editor) {
      this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isLt2M;
    },
    submit(){
      this.$refs.placeCodeForm.validate(valid => {
        if (valid) {
          const loading=this.$loading({
            text:'提交中'
          })
          if(this.form.id==null)
          {
            this.form.id = 0;
          }
          this.form.fatherId = this.id
          const form=JSON.parse(JSON.stringify(this.form));
          editIncubatorsArticleApi(form).then((res)=>{
            this.$message.success('保存成功');
            this.closeDialog(1);
          }).catch(()=>{
            this.$message.error('保存失败');
          }).finally(()=>{
            loading.close();
          })
        } else {
          return false
        }
      })
    },
    getDetail(){
      getIncubatorsArticleByFatherIdApi({fatherId:this.id,type:1}).then((res)=>{
        if(res)
        {
          this.form=res;
        }
      }).finally(()=>{
        this.formLoading=false;
      })
    },
  },
  watch: {

  },
  mounted() {
    if(this.id){
      this.getDetail();
    }

  },

}
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #c5c5c5;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;

}
.w-e-bar.w-e-bar-show.w-e-toolbar{
  border: 1px solid;
}
.w-e-scroll{
  border: 1px solid;
}
</style>
<style scoped>
/*
*让select撑开
*/
.el-select,.el-cascader{
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed #c5c5c5;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;

}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
